<?
include('./inc/master.php');

#Get latest uploaded schedule
$sql = "SELECT MAX(schedule_id) AS max_id FROM schedules";
$result = $db->query($sql);
$row = $result->fetch_object();
$max_id = $row->max_id;

$error;
$id;
$prev_id;
$next_id;

#Set schedule id to either...
#...a provided value
if($_POST['id'])
    $id = $_POST['id'];
#...a provided month/year's first schedule
elseif($_POST['month']) {
    $sql = "SELECT schedule_id from schedules WHERE DATE_FORMAT(date,'%Y%M') = '" . $_POST['year'] . $_POST['month']."' ORDER BY schedule_id ASC LIMIT 0,1 " ; 
    $result = $db->query($sql);
    $row = $result->fetch_object();
    if(!$id = $row->schedule_id)
      $error = "Sorry, there are no schedules for ".$_POST['month']." ".$_POST['year'];
}
#...this weeks value
else {
    $sql = "
      SELECT schedule_id FROM schedules 
      WHERE date = CURDATE() OR date = (SELECT MAX(date) FROM schedules) 
      LIMIT 0,1";
    $result = $db->query($sql);
    $row = $result->fetch_object();
    $id = $row->schedule_id;
}


#error checking
if ($id > $max_id || $id < 1) {
    if (!$error) 
      $error = "That schedule doesn't exist";
    $id = $max_id;
}
#sees if the next and previous schedules exist
#and set variables if they do
if($id < $max_id and $id > 0) 
    $next_id = $id + 1;
if($id > 1)
    $prev_id = $id - 1;


#Find the schedule referenced by the above schedule id ($id)
$sql = "
    SELECT a.start, a.end, a.date, a.open, a.attended, a.name AS name1, users.name AS name2
    FROM (
        SELECT schedules.user_id_2, schedules.start, schedules.end, schedules.date, schedules.open, schedules.attended, users.name
        FROM schedules
        INNER JOIN users ON users.user_id = schedules.user_id_1
        WHERE schedules.schedule_id = $id
    ) AS a
    INNER JOIN users ON users.user_id = a.user_id_2
    ORDER BY a.start, a.date ASC";

$result = $db->query($sql);
$schedule_rows;

if($result) {
	while($row = $result->fetch_object()) {
        $short = & $schedule_rows[date('g',mktime($row->start)) . ' - ' . date('g',mktime($row->end))][$row->date];
        $short['ampm'] = date('a',mktime($row->start));
        
        $now = time();
        $then = mktime($row->start,0,0,substr($row->date,5,2),substr($row->date,8,2),substr($row->date,0,4));
        
        #if open = 0, no open shifts
        #if open = 1, name1's shift is open
        #if open = 2, name2's shift is open

        if($row->name1 and ($then < $now || ($row->open != 1 and $row->open != 3))) {
          $short['name1'] = $row->name1;
          $short['link1'] = "make-open.php";          
        }
        else {
          $short['name1'] = 'Open';          
          $short['link1'] = "take-open.php";  
        }

        if($row->name2 and ($then < $now || ($row->open != 2 and $row->open != 3))) {
          $short['name2'] = $row->name2;          
          $short['link2'] = "make-open.php";   
        }
        else{
          $short['name2'] = 'Open';          
          $short['link2'] = "take-open.php";
        }
          
          
        #mark classes
        if($row->open > 0)
          $short['class'] = 'open ';
        if($then < $now)
          $short['class'] = 'past ';
          
        $short['open'] = $row->open;
    }
}




#Get date information for headers of the displayed schedule table
$dates;
$sql = "SELECT DISTINCT date FROM schedules WHERE schedule_id = $id ORDER BY date ASC";

if($result = $db->query($sql))
  while($row = $result->fetch_object()) {
    $timestamp = mktime(0,0,0,substr($row->date,5,2),substr($row->date,8,2),substr($row->date,0,4));
    $dates[] = array(date('l',$timestamp) => date('n/j',$timestamp));
  }




# Get spanning years to populate drop down box
$year_span;
$sql = "SELECT DISTINCT DATE_FORMAT( date, '%Y' ) AS date FROM schedules ORDER BY date DESC";
$result = $db->query($sql);
while($row = $result->fetch_object())
  $year_span[] = $row->date;





#Populate templates
$smarty->assign('prev_id', $prev_id);
$smarty->assign('next_id', $next_id); 
$smarty->assign('dates',$dates);
$smarty->assign('schedule_rows',$schedule_rows);
$smarty->assign('error',$error);
$smarty->assign('id',$id);

#If this script was called by ajax we return just a populated schedule table
if($_POST['ajax'])
  $smarty->display('schedule.tpl');  
#Otherwise give the entire page
else {
  $smarty->assign('years', $year_span);
  $smarty->assign('title','Schedule');
  $smarty->assign('schedule', 1);
  $smarty->display('schedule.base.tpl');
}
?>
